

#ifndef __MATERIALVIEW_H__
#define __MATERIALVIEW_H__

#include <baseeditor.h>
#include <baseitemmodel.h>
#include <baseview.h>
#include <materialmanager.h>
#include <material_editor/basematerialeditor.h>
#include <material_editor/materialviewitems.h>
#include <QAction>
#include <QMap>
#include <QWidget>
#include <QSplitter>
#include <QTabWidget>
#include <QTreeView>


class MaterialView : public BaseView
{
  Q_OBJECT
public:
  enum Action
  {
    Action_Save,
    Action_SaveAll,

    Action_TextureAdd,
    Action_TextureEdit,


    Action_ShaderAdd,
    Action_ShaderEdit,
    Action_ShaderChangeType,

    Action_ProgramAdd,
    Action_ProgramEdit,

    Action_EffectAdd,
    Action_EffectEdit,

    Action_MaterialAdd,
    Action_MaterialEdit,

    Action_MaterialDataRename,
    Action_MaterialDataDelete,
    Action_MaterialDataGroupRename,
    Action_MaterialDataGroupDelete,

    Action_COUNT

  };

private:

  QMap<QString, CategoryItem*>  _items;

  QTabWidget    *_editorPanels;
  QTreeView     *_overview;


  MaterialManager *_manager;
  BaseItemModel *_overviewModel;

  QAction*  _actions[Action_COUNT];

  bool      _selectNewItems;

  void InitGUI ();
  void InitActions ();
  void InitModel ();

  QMap<void*, BaseEditorWidget*> _editors;

  BaseEditorWidget* OpenEditor (void* materialData);
  void InsertEditor (BaseMaterialEditorWidget* editor);
  void RemoveEditorWithData (void* data);
  void SelectItem (BaseItem* item);
  void DirectSelectItem (BaseItem* item);

  BaseItem* GetItem (MaterialData* materialItem);
  CategoryItem* GetCategoryItem (MaterialData* materialItem);

  void AddItem(BaseItem* item, MaterialData* materialItem);

  void EnableDisableActions (BaseItem* item);
 
private slots:

  void SelectionChanged (const QModelIndex& newIndex, const QModelIndex& oldIndex);
  void ContextMenuRequested (const QPoint& point);
  void ItemDoubleClicked (const QModelIndex& idx);


  void MaterialDataRenameCurrent ();
  void MaterialDataRemoveCurrent ();

  void MaterialDataGroupRename ();
  void MaterialDataGroupRemove ();

  /**
   * \name Eventhandling from MaterialManager
   * @{
   */

  void MaterialDataChanged (MaterialData* item);
  void MaterialDataRemoved (MaterialData* item);
  void TextureAdded (Texture* texture);
  void ShaderAdded (Shader* shader);
  void ProgramAdded (Program* shader);
  void EffectAdded (Effect* effect);
  void MaterialAdded (Material* material);


  /**
   * @}
   */

  /**
   * \name Texturehandling from Actions
   * @{
   */
  void TextureEditCurrent ();
  void TextureAdd ();
  void TextureEdit(Texture* texture);
  /**
   * @}
   */

  /**
   * \name Shaderhandling from Actions
   * @{
   */
  void ShaderEditCurrent ();
  void ShaderChangeTypeCurrent ();
  void ShaderAdd ();
  void ShaderEdit(Shader* shader, int line = 0);
  /**
   * @}
   */

  /**
   * \name Programhandling from Actions
   * @{
   */
  void ProgramEditCurrent ();
  void ProgramAdd ();
  void ProgramEdit(Program* program);
  /**
   * @}
   */

  /**
   * \name Effecthandling from Actions
   * @{
   */
  void EffectEditCurrent ();
  void EffectAdd ();
  void EffectEdit(Effect* effect);
  /**
   * @}
   */

  /**
   * \name Materialhandling from Actions
   * @{
   */
  void MaterialEditCurrent ();
  void MaterialAdd ();
  void MaterialEdit(Material* material);
  /**
   * @}
   */


  void UpdateEditorLabel (BaseEditorWidget* editor);
  void EditorDirtyChanged (bool dirty);
  void EditorNameChanged (const QString& name);

  void TabCloseRequested (int index);

  void Save ();
  void SaveAll ();
  void Compile ();

public:
  MaterialView (MaterialManager *manager, QWidget* parent = 0);

  MaterialManager* GetMaterialManager () { return _manager; }

  QAction* GetAction(Action action) { return _actions[action]; }

  virtual void ConnectSignals ();
  virtual void CreateMenu(MainWindow *mainWindow);

  void EditTexture (Texture* texture);
  void EditShader (Shader* shader, int line = 0);
  void EditProgram(Program* program);
  void EditEffect(Effect* effect);
  void EditMaterial(Material* material);
};



template<class T>
T* get_current_item (QTreeView* view)
{
  QModelIndex idx = view->currentIndex ();
  if (!idx.isValid ())
    {
      return 0;
    }

  try
    {
      BaseItem* item = static_cast<BaseItem*>(idx.internalPointer ());
      return reinterpret_cast<T*>(item);
    }
  catch (std::exception& e)
    {
    }
  return 0;
}


#endif /* ! __MATERIALVIEW_H__ */



